<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:f="livcos.org/ns/xslt/function"
	xmlns:xs="http://www.w3.org/2001/XMLSchema"
	exclude-result-prefixes="f xs">
	
	<xsl:import href="../common/ide_dummy.xsl"/>
	
	<xsl:param name="basePath"/>
	
	<xsl:variable name="defaults">
		<view ext="html">/livcos.org/LivCos/web/public</view>
		<view ext="html" prefix="map/">/livcos.org/web/map</view>
		<view ext="pdf">/livcos.org/page/doc</view>
		<view ext="jpg" access="direct">/livcos.org/web</view>
		<view ext="gif" access="direct">/livcos.org/web</view>
		<view ext="png" access="direct">/livcos.org/web</view>
		<view ext="css" access="direct">/livcos.org/web</view>
		<view ext="js" access="direct">/livcos.org/web</view>
	</xsl:variable>
	
	<xsl:variable name="favicon">/livcos.org/web/img/favicon</xsl:variable>
	<xsl:variable name="robots">/localhost/data/robots</xsl:variable>
	<xsl:variable name="index">/livcos.org/web/redirect</xsl:variable>
	
	<!-- resolve URIs -->
	
	<xsl:template match="uri" mode="#all">
		<xsl:variable name="defaultView" as="element()?">
			<xsl:variable name="ds" select="$defaults/view[@ext = current()/@ext]"/>
			<xsl:variable name="d" select="$ds[@prefix and starts-with(current()/@relativeName, @prefix)]"/>
			<xsl:sequence select="if ($d) then $d else $ds[1]"/>
		</xsl:variable>
		<xsl:choose>
			<xsl:when test="$defaultView/@access = 'direct'">
				<uri full="{$defaultView}{@path}/{@name}.{@ext}" path="{$defaultView}{@path}">
					<xsl:copy-of select="@ext | @name | @file | @fragment"/>
					<xsl:copy-of select="*"/>
				</uri>
			</xsl:when>
			<xsl:when test="$defaultView">
				<xsl:variable name="abs" select="concat($defaultView, @path)"/>
				<uri full="{$abs}/{@name}.{@ext}" path="{$abs}">
					<xsl:copy-of select="@ext | @name | @file | @fragment"/>
					<xsl:copy-of select="*"/>
				</uri>
			</xsl:when>
			<xsl:otherwise>
				<xsl:call-template name="standard-access"/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
	
	<xsl:template match="uri[@full = '/favicon.ico']">
		<access obj="{$favicon}" scope="content"/>
	</xsl:template>
	
	<xsl:template match="uri[(@full = '/robots.txt')]">
		<access obj="{$robots}" scope="view"/>
	</xsl:template>
	
	<xsl:template match="uri[(@full = '/cb.job')]">
		<access obj="/livcos.org/web/edit/ctrl/cb" scope="view"/>
	</xsl:template>
	
	<xsl:template match="uri[(@full = '/tools.html')]">
		<access obj="/livcos.org/web/admin/tools" scope="view"/>
	</xsl:template>
	
	<xsl:template match="uri[(@full = '/') or (@full = '/index.html')]">
		<access obj="{$index}" scope="view"/>
	</xsl:template>
	
	<xsl:template name="standard-access">
		<access obj="{@path}/{@name}">
			<xsl:if test="normalize-space(@ext) != ''">
				<xsl:attribute name="scope">
					<xsl:choose>
						<xsl:when test="@ext = 'view'">view</xsl:when>
						<xsl:when test="@ext = 'job'">view</xsl:when>
						<xsl:when test="@ext = 'cosmos'">cosmos</xsl:when>
						<xsl:when test="@ext = 'content'">content</xsl:when>
						<xsl:when test="@ext = 'plain'">content</xsl:when>
						<xsl:when test="@ext = 'meta'">meta</xsl:when>
						<xsl:when test="@ext = 'cache'">cache</xsl:when>
						<xsl:when test="@ext = 'history'">history</xsl:when>
						<xsl:when test="@ext = 'data'">data</xsl:when>
						<xsl:when test="@ext = 'xsl'">content</xsl:when>
						<xsl:when test="@ext = 'xslt'">content</xsl:when>
						<xsl:otherwise>view</xsl:otherwise>
					</xsl:choose>
				</xsl:attribute>
			</xsl:if>
		</access>
	</xsl:template>
	
	<!-- inverse resolver to build an URI in it's shortest form from a reference -->
	
	<xsl:template match="ref">
		<xsl:variable name="dv" as="element()?">
			<xsl:variable name="ds" select="$defaults/view[@ext = current()/@ext]"/>
			<xsl:variable name="d" select="$ds[@prefix and starts-with(@data, concat('/', @prefix))]"/>
			<xsl:sequence select="if ($d) then $d else $ds[1]"/>
		</xsl:variable>
		<xsl:value-of	select="if ($dv and starts-with(@view, $dv)) then substring-after(@view, $dv) else @view"/>
		<xsl:variable name="dd">
			<xsl:call-template name="diffPath">
				<xsl:with-param name="str" select="@data"/>
				<xsl:with-param name="compare" select="@view"/>
			</xsl:call-template>
		</xsl:variable>
		<xsl:text>/</xsl:text>
		<xsl:value-of select="f:cut-start($dd, 'data/')"/>
		<xsl:if test="normalize-space(@ext) != ''">
			<xsl:text>.</xsl:text>
			<xsl:value-of select="@ext"/>
		</xsl:if>
	</xsl:template>
	
	<xsl:template match="ref[not(@view)]">
		<xsl:value-of select="@uri"/>
	</xsl:template>
	
	<xsl:template name="diffPath">
		<xsl:param name="str"/>
		<xsl:param name="compare"/>
		<xsl:variable name="tk" select="substring-before($str, '/')"/>
		<xsl:choose>
			<xsl:when test="not(contains($str, '/'))">
				<xsl:value-of select="$str"/>
			</xsl:when>
			<xsl:when test="$tk = $compare">
				<xsl:value-of select="substring-after($str, '/')"/>
			</xsl:when>
			<xsl:when test="$tk = substring-before($compare, '/')">
				<xsl:call-template name="diffPath">
					<xsl:with-param name="str" select="substring-after($str, '/')"/>
					<xsl:with-param name="compare" select="substring-after($compare, '/')"/>
				</xsl:call-template>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="$str"/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
	
	<!-- independend helper functions -->
	
	<xsl:function name="f:find-data-object">
		<xsl:param name="ref" as="xs:string*"/>
		<xsl:variable name="_ref" select="f:cut-start($ref, '/')"/>
		<xsl:choose>
			<xsl:when test="normalize-space($_ref) = ''">
			</xsl:when>
			<xsl:when test="f:object-available(concat('/', $_ref))">
				<xsl:value-of select="concat('/', $_ref)"/>
			</xsl:when>
			<xsl:when test="contains($ref, '/data/')">
				<xsl:value-of select="f:find-object('/', $basePath, $_ref)"/>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="f:find-object('/', $basePath, concat('data/', f:cut-start($_ref, 'data/')))"/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:function>
	
	<xsl:function name="f:cut-start" as="xs:string">
		<xsl:param name="str"/>
		<xsl:param name="subStr"/>
		<xsl:value-of	select="if (starts-with($str, $subStr)) then substring($str, string-length($subStr)) else $str"/>
	</xsl:function>
	
	<xsl:function name="f:cut-end" as="xs:string">
		<xsl:param name="str"/>
		<xsl:param name="subStr"/>
		<xsl:value-of
			select="if (ends-with($str, $subStr)) then substring($str, 1, string-length($str) - string-length($subStr)) else $str"
		/>
	</xsl:function>
	
</xsl:stylesheet>
